var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require("lodash");

/* GET home page. */
router.post('/',upload.array(), function(req, res, next) {
    let data = {
        isSuccess: false,
        data: null,
        isRecorded: false,
        msg: '判断失败！'
    };
    // 这里要做处理，防止sql注入
    let zcbh = _.trim(String(req.body.zcbh));
    let tmbh = _.trim(String(req.body.tmbh));
    let whereClause = '';
    if(zcbh) {
        whereClause = "zcbh='" + zcbh + "'";
    }
    if(tmbh) {
        whereClause = "tmbh='" + tmbh + "'";
    }
    let sql = 'select * from equipmentinfo where ' + whereClause;
    console.log(sql)
    db.execute(sql, function(err, rows) {
        if(err) {
            return res.json(data);
        } else {
            data.isSuccess = true;
            if(rows.length > 0)  {
                data.isRecorded = true;
                data.data = rows[0];
                data.msg = "该设备已录入";
                return res.json(data);
            } else if(rows.length < 1){
                data.isRecorded = false;
                data.msg = "该设备未录入";

                var sql = 'select zybbh from equipmentinfo order by id DESC limit 1;'
                db.execute(sql, function(err, rows) {
                    if(err) {
                        console.log('最新资产数据取回错误！' + err);
                    } else {
                        if(rows && rows.length > 0) {
                            data.data = Number(rows[0].zybbh) + 1;
                        } else {
                            data.data = 1;
                        }
                    }
                    console.log(rows);
                    console.log(data);
                    return res.json(data);
                })
            }
        }
    })
});



module.exports = router;